﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Windows8Accelerator.Core.Xaml.Controls;

namespace Windows8Accelerator.Core.Xaml.Helpers
{
    public class WebHelper : WebHelperBase
    {
        protected override async Task<string> InternalGetStringAsync(Uri uri, string accept = null, string user = null, string password = null)
        {
            var httpWebRequest = HttpWebRequest.CreateHttp(uri);
            if (accept != null)
                httpWebRequest.Accept = accept;
            if (user != null)
            { 
                httpWebRequest.Credentials = new NetworkCredential(user, password);
            }

            var response = await Task<WebResponse>.Factory.FromAsync(httpWebRequest.BeginGetResponse, httpWebRequest.EndGetResponse, null) as HttpWebResponse;
            using (response)
            {
                EnsureSuccessStatusCode(response);
                using (var reader = new StreamReader(response.GetResponseStream()))
                {
                    return await reader.ReadToEndAsync();
                }
            }
        }

        private void EnsureSuccessStatusCode(HttpWebResponse response)
        {
        }

        protected async override Task<string> InternalPostStringAsync(Uri uri, object postData, string accept, string user, string password)
        {
            var httpWebRequest = HttpWebRequest.CreateHttp(uri);
            httpWebRequest.Method = "POST";

            if (postData is IEnumerable<KeyValuePair<string, string>>)
            {
                throw new NotImplementedException();
                //httpContent = new FormUrlEncodedContent(postData as IEnumerable<KeyValuePair<string, string>>);
            }
            else
            {
                httpWebRequest.ContentType = "application/json";
                //httpWebRequest.Headers["datatype"] = "jsondata";
                //httpWebRequest.Headers["async"] = "false";
                var requestStream = await Task<Stream>.Factory.FromAsync(httpWebRequest.BeginGetRequestStream, httpWebRequest.EndGetRequestStream, null);
                using (var streamWriter = new StreamWriter(requestStream))
                {
                    streamWriter.Write(postData);
                }
            }


            if (accept != null)
                httpWebRequest.Accept = accept;
            if (user != null)
            {
                httpWebRequest.Credentials = new NetworkCredential(user, password);
            }

            var response = await Task<WebResponse>.Factory.FromAsync(httpWebRequest.BeginGetResponse, httpWebRequest.EndGetResponse, null) as HttpWebResponse;
            using (response)
            {
                EnsureSuccessStatusCode(response);
                using (var reader = new StreamReader(response.GetResponseStream()))
                {
                    return await reader.ReadToEndAsync();
                }
            }
        }

        public override async Task<Stream> GetStreamAsync(Uri uri)
        {
            var httpWebRequest = HttpWebRequest.CreateHttp(uri);
            var response = await Task<WebResponse>.Factory.FromAsync(httpWebRequest.BeginGetResponse, httpWebRequest.EndGetResponse, null) as HttpWebResponse;
            using (response)
            {
                EnsureSuccessStatusCode(response);
                var stream = new MemoryStream();
                await response.GetResponseStream().CopyToAsync(stream);
                stream.Position = 0;
                return stream;
            }
        }

    }
}
